﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;

namespace eProject.Web.UI.Admin
{
    public partial class AirportManagementEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                loadData();
            }
        }
        private void loadData() {
            List<Cities> city = Cities.GetAll();
            ddlCity.DataSource = city;
            ddlCity.DataTextField = "Name";
            ddlCity.DataValueField = "Id";
            ddlCity.DataBind();
            int id = int.Parse(Request.QueryString["Id"].ToString());
            AirPorts airport = new AirPorts(id);
            txtAirportCode.Text = airport.AirPortId;
            txtAirportName.Text = airport.Name;
            lblCreateDate.Text = airport.CreatedDate.ToShortDateString();
            lblLastUpdate.Text = airport.LastUpdate.ToShortDateString();
        }
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnSubmit.ServerClick += new EventHandler(btnSubmit_Click);
            this.btnReset.ServerClick += new EventHandler(btnReset_Click);
        }
        protected void btnSubmit_Click(object sender, EventArgs e) {
            try
            {
                int id = int.Parse(Request.QueryString["Id"].ToString());
                string airportId = txtAirportCode.Text.Trim().ToString();
                string airportName = txtAirportName.Text.Trim().ToString();
                int cityId = int.Parse(ddlCity.SelectedValue.ToString());
                DateTime lastedit = DateTime.Now;
                AirPorts air = new AirPorts(id);
                air.AirPortId = airportId;
                air.Name = airportName;
                air.CityId = cityId;
                air.LastUpdate = lastedit;
                air.Save();
            }catch(Exception ex){
                throw ex;
            }
            Response.Redirect("AirportsManagementPro.aspx");
            //Response.Write(id);
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtAirportCode.Text = "";
            txtAirportName.Text = "";
        }
    }
}